//
//  SlideView.m
//  SmartContorary
//
//  Created by enghou on 17/6/5.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

#import "SlidePageView.h"



@implementation SlidePageView
{
    UIView *line;
    NSInteger currentIdx;
    NSInteger oldIndex;
    NSArray *titleArray;
    CGFloat offX;
    BOOL block;
    NSArray *btns;
    UIView *bottomline;
    CGFloat previousOffset;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderColor = [[UIColor colorWithHexString:@"eeeeee"]CGColor];
        self.layer.borderWidth = 1;
    }
    return self;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

-(void)unlockForRespondsToScroll{
    block = NO;
}

-(void)configTitles:(NSArray *)array{
    previousOffset = [[UIScreen mainScreen]bounds].size.width;
    titleArray = array;
    offX = 0;
    if ([array count]>0) {
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL *stop) {
            [obj removeFromSuperview];
        }];
        CGFloat width = [[UIScreen mainScreen]bounds].size.width / array.count;
        int i = 0;
        NSMutableArray *arr = [NSMutableArray array];
        for (NSString *title in array) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            [btn setTitle:title forState:UIControlStateNormal];
            btn.tag = 1000 + i;
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            if (i == 0) {
                [btn setTitleColor:[UIColor colorWithHexString:@"004ea2"] forState:UIControlStateNormal];
            }else{
                [btn setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
            }
            [btn setFrame:CGRectMake(width * i, 0, width, self.frame.size.height - 2.5)];
            [btn addTarget:self action:@selector(slideToIndex:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            [arr addObject:btn];
            i++;
        }
        btns = [arr copy];
        CGFloat wid = self.frame.size.width / [array count] / 2;
        CGFloat originX = wid / 2;
        line = [[UIView alloc]initWithFrame:CGRectMake(originX, self.frame.size.height - 2.5, wid, 2.5)];
        line.backgroundColor = [UIColor colorWithHexString:@"004ea2"];
        [self addSubview:line];
        bottomline = [UIView new];
        bottomline.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        [self addSubview:bottomline];
        currentIdx = 0;
    }
}

-(void)slideToPosition:(NSInteger)index{
    UIButton *btn = (UIButton *)[self viewWithTag:1000 + index];
    if (btn) {
        [self slideToIndex:btn];
    }
}

-(void)slideToOffset:(CGFloat)x{
    if (!block) {
        CGFloat convertedX = x / 5;
        line.frame = CGRectMake(offX + convertedX, line.frame.origin.y, line.frame.size.width, line.frame.size.height);
    }
}

-(void)slideToIndex:(UIButton *)btn{
    NSInteger index = btn.tag - 1000;
    currentIdx = index;
    for(UIButton *bt in self.subviews){
        if ([bt isKindOfClass:[UIButton class]]) {
            [bt setTitleColor:[UIColor colorWithHexString:@"999999"] forState:UIControlStateNormal];
        }
    }
    [btn setTitleColor:[UIColor colorWithHexString:@"004ea2"] forState:UIControlStateNormal];
    CGFloat itemWidth = self.frame.size.width / [titleArray count];
    CGFloat originX = index * itemWidth + itemWidth / 4;
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.25];
    line.frame = CGRectMake(originX, line.frame.origin.y, line.frame.size.width, line.frame.size.height);
    [UIView commitAnimations];
    [self.delegate changeToPage:index];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat btnwidth = self.frame.size.width / btns.count;
    for (int i = 0; i < btns.count; i++) {
        UIButton *button = [btns objectAtIndex:i];
        [button setFrame:CGRectMake(btnwidth * i, 0, btnwidth, self.frame.size.height - 2.5)];
    }
    
    bottomline.frame = CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1);
    if (titleArray.count != 0) {
        line.frame = CGRectMake(currentIdx * self.frame.size.width / titleArray.count + self.frame.size.width / (4 * [titleArray count]), line.frame.origin.y, line.frame.size.width, line.frame.size.height);
        oldIndex = currentIdx;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}

@end
